<?php
// 订单模型
class OrderModel extends RelationModel {
	
	protected $_link = array(
			'OrderList'=>array(
					'mapping_type'=>HAS_MANY,//HAS_ONE查询出一条
					'class_name'=>'OrderList',//表名
					'mapping_name'=>'OrderList',//表名
					'foreign_key'=>'LS_DDBH',//外键
			 )	
			);
	
	
	protected $_auto =	array(
			array('DD_BH','getOrderBH',1,'callback'),//自动生成订单编号
	);
		
	//自动生成订单编号（报名单号）
	public function getOrderBH($DD_ZD,$DD_CJSJ){
		
// 		$DD_ZD = $_POST['DD_ZD'];//订单来源 校区编号
// 		$DD_CJSJ = $_POST['DD_CJSJ'];//日期 
		$DD_CJSJ = str_replace("-", "", $DD_CJSJ);
 		$DD_CJSJ = substr($DD_CJSJ, 0, 8);
		
		$DD_BH = $DD_ZD . $DD_CJSJ  ;
		$map['DD_BH'] = array('like',"%".$DD_BH."%");
		$Order = M ( "Order" );
		$Orders = array();
		$Orders = $Order->where($map)->limit (0,1)->order ( 'DD_BH desc' )->select();
		if(count($Orders)!=0){
			$ord = $Orders[0];
			$maxBH = $ord['DD_BH'];
			$lastNum = intval(substr($maxBH,-5));//后5位流水号
			$lastNum += 1;
			if($lastNum<10){
				$DD_BH = $DD_BH.'0000'.$lastNum;
			}else if($lastNum<100){
				$DD_BH = $DD_BH.'000'.$lastNum;
			}else if($lastNum<1000){
				$DD_BH = $DD_BH.'00'.$lastNum;
			}else{
				$DD_BH = $DD_BH.$lastNum;
			}
		}else{
			$DD_BH = $DD_BH . '00001' ;
		}
		return  $DD_BH;
	}
	
}